package com.dap.controller;


import com.dap.service.FlowControlService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

@RestController
@RequestMapping("/FlowControl")
public class FlowControlController {

    @Autowired
    private FlowControlService flowControlService;


    /**
     *在一定时间内，顺利执行的请求数/总的请求数（比率）
     * @param json  包含时间间隔，应用，服务，组等信息
     * @return
     */
    @RequestMapping(value = "/getRate",method = RequestMethod.POST)
    public String getFlowControlRate(@RequestBody String json){



        //调试代码
        System.out.println("请求来了");


        ObjectMapper om = new ObjectMapper();

        HashMap<String ,String> hashMap =null;
        //json转mp
        try {
            hashMap = om.readValue(json, HashMap.class);

        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(hashMap.get("time"));
        Long interval = Long.valueOf(hashMap.get("time"));


        String applicationName = hashMap.get("application");
        String interfaceName = hashMap.get("interface");
        String groupName = hashMap.get("group");


        String flowControlRate = flowControlService.getFlowControlRate(interval, applicationName,interfaceName,groupName);

        return flowControlRate;
    }

    /**
     * 一定时间内顺利执行的请求数（未被限流的）
     * @param time
     * @param applicationName  应用名
     * @param interfaceName  接口名
     * @param groupName  组名
     * @return
     */
    @RequestMapping(value = "/getCountSuccess",method = RequestMethod.POST)
    public String getCountSuccess(String time, String applicationName,String interfaceName,String groupName){

        Long interval = Long.parseLong(time);

        String s = flowControlService.countSuccess(interval, applicationName,interfaceName,groupName);

        return s;
    }


    /**
     * 当前请求在队列中的位置
     * @param applicationName  应用名
     * @param interfaceName  接口名
     * @param groupName  组名
     * @return
     */
    @RequestMapping(value = "/getRank",method = RequestMethod.POST)
    public String rankOnQueue(String applicationName,String interfaceName,String groupName){
        String rank = flowControlService.rankOnQueue(applicationName,interfaceName,groupName);
        return rank;
    }

    /**
     * 一段时间内的并发数
     * @param time
     * @param applicationName  应用名
     * @param interfaceName  接口名
     * @param groupName  组名
     * @return
     */
    @RequestMapping(value = "/getConcurrence",method = RequestMethod.POST)
    public String getConcurrence(String time ,String applicationName,String interfaceName,String groupName){
        Long interval = Long.parseLong(time);
        Long concurrence = flowControlService.concurrence(interval, applicationName,interfaceName,groupName);
        String s = concurrence.toString();
        return s;
    }








}
